﻿using MxWeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class user_parent : MxWeiXinPF.Web.UI.ManagePage
    {
        protected string action = MXEnums.ActionEnum.Add.ToString();
        private int id = 0;
        private int wid = 0;

        protected void bnSelectUser_Click(object sender, EventArgs e)
        {
            base.ChkAdminLevel("user_parent", MXEnums.ActionEnum.View.ToString());

            if (this.tbparentname.Text.Equals(""))
            {
                base.JscriptMsg("用户过多，请输入关键字查询，支持模糊查找！", "", "Error");
            }
            else
            {
                this.gv_DataBindUser();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string cmdText = "sp_usersparentopenid";
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBString"].ConnectionString);
            SqlCommand command = new SqlCommand(cmdText, connection)
            {
                CommandType = CommandType.StoredProcedure
            };
            command.Parameters.Add("@ID", SqlDbType.Int);
            command.Parameters["@ID"].Value = this.id;
            if (!this.hopenid.Value.Equals(""))
            {
                command.Parameters.Add("@parentopenid", SqlDbType.NVarChar, 50);
                command.Parameters["@parentopenid"].Value = this.hopenid.Value;
            }
            try
            {
                connection.Open();
                command.ExecuteNonQuery();
                base.JscriptMsg("指定成功！", "user_list.aspx", "Success");
            }
            catch (SqlException exception)
            {
                base.JscriptMsg(exception.Errors[0].Message, "user_list.aspx", "Error");
            }
            catch (Exception exception2)
            {
                base.JscriptMsg(exception2.Message, "", "Error");
            }
            finally
            {
                connection.Close();
            }
        }

        protected void dgUser_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "SelectName")
            {
                int itemIndex = e.Item.ItemIndex;
                this.tbparentname.Text = this.dgUser.Items[itemIndex].Cells[2].Text;
                this.hopenid.Value = this.dgUser.Items[itemIndex].Cells[1].Text;
                this.hid.Value = this.dgUser.Items[0].Cells[0].Text;
                this.DivDoctorList.Style["Display"] = "None";
            }
        }

        private void gv_DataBindUser()
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBString"].ConnectionString))
            {
                connection.Open();
                string selectCommandText = "[sp_FindUser]";
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, connection)
                    {
                        SelectCommand = { CommandType = CommandType.StoredProcedure }
                    };
                    if (!this.tbparentname.Text.Equals(""))
                    {
                        adapter.SelectCommand.Parameters.Add(new SqlParameter("@nickname", SqlDbType.VarChar, 50));
                        adapter.SelectCommand.Parameters["@nickname"].Value = this.tbparentname.Text.Trim();
                    }
                    if (!(this.wid.Equals("") || (this.wid == 0)))
                    {
                        adapter.SelectCommand.Parameters.Add(new SqlParameter("@wid", SqlDbType.Int));
                        adapter.SelectCommand.Parameters["@wid"].Value = this.wid;
                    }
                    adapter.SelectCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
                    adapter.SelectCommand.Parameters["@id"].Value = this.id;
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "Table");
                    this.dgUser.DataSource = dataSet;
                    this.dgUser.DataBind();
                    if (this.dgUser.Items.Count == 1)
                    {
                        this.tbparentname.Text = this.dgUser.Items[0].Cells[2].Text;
                        this.hopenid.Value = this.dgUser.Items[0].Cells[1].Text;
                        this.hid.Value = this.dgUser.Items[0].Cells[0].Text;
                        this.DivDoctorList.Style["Display"] = "None";
                    }
                    else if (this.dgUser.Items.Count > 1)
                    {
                        this.DivDoctorList.Style["Display"] = "Block";
                    }
                    else
                    {
                        base.JscriptMsg("找不到跟输入内容相关的内容，请重新输入！", "", "Success");
                    }
                }
                catch (SqlException exception)
                {
                    base.JscriptMsg(exception.Errors[0].Message, "", "Error");
                }
                catch (Exception exception2)
                {
                    base.JscriptMsg(exception2.Message, "", "Error");
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            this.DivDoctorList.Style["Display"] = "None";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string queryString = MXRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(queryString) && (queryString == MXEnums.ActionEnum.Edit.ToString()))
            {
                this.action = MXEnums.ActionEnum.Edit.ToString();
                this.id = MXRequest.GetQueryInt("id");
                this.wid = MXRequest.GetQueryInt("wid");
                if (this.id == 0)
                {
                    base.JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new BLL.users().Exists(this.id))
                {
                    base.JscriptMsg("信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!this.Page.IsPostBack)
            {
                base.ChkAdminLevel("user_parent", MXEnums.ActionEnum.View.ToString());
                if (this.action == MXEnums.ActionEnum.Edit.ToString())
                {
                    this.ShowInfo(this.id);
                }
            }
        }

        private void ShowInfo(int _id)
        {
            base.ChkAdminLevel("user_parent", MXEnums.ActionEnum.View.ToString());
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBString"].ConnectionString);
            SqlCommand command = new SqlCommand("select a.*,isnull(b.nick_name,b.wxName) parentname from dt_users a left join dt_users b on a.parentopenid=b.wxopenid where a.ID=" + _id, connection);
            connection.Open();
            try
            {
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    this.tbparentname.Text = reader["parentname"].ToString();
                    this.hopenid.Value = reader["parentopenid"].ToString();
                    this.txtNickName.Text = reader["nick_name"].ToString();
                    this.img.ImageUrl = reader["avatar"].ToString();
                    this.hid.Value = reader["id"].ToString();
                }
                reader.Close();
            }
            catch (SqlException exception)
            {
                base.JscriptMsg(exception.Errors[0].Message, "", "Error");
            }
            catch (Exception exception2)
            {
                base.JscriptMsg(exception2.Message, "", "Error");
            }
            finally
            {
                connection.Close();
            }
        }
    }
}